<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="props.width" @ok="handleSubmit">
    <BasicForm @register="registerForm" name="CardTypForm">
      <template #Discount="{ model, field }"> <a-input v-model:value="model[field]" placeholder="折扣比" addon-after="%" /> </template>
      <template #User="{ model, field }">
        <a-select ref="select" v-model:value="model[field]">
          <a-select-option :value="item.id" v-for="(item, index) in UserList" :key="index">{{ item.nickName }}</a-select-option>
        </a-select>
      </template>
    </BasicForm>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema2 } from '../CardTyp.data';
  import { saveOrUpdate, UserListApi, createUser } from '../CardTyp.api';

  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const isDetail = ref(false);
  const isCopy = ref(false);
  const iD = ref('');
  const arr = ref<string[]>([]);
  const UserList = ref<any[]>([]);
  const inputIsShow = ref(false);
  const str = ref('');
  // 获取父级组件值
  const props = defineProps({
    width: {
      type: Number,
      default: 800,
    },
  });

  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate, scrollToField }] = useForm({
    labelWidth: 150,
    schemas: formSchema2,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
  });

  UserListApi({ pageNo: 1, pageSize: 9999 }).then((res) => {
    UserList.value = res;
  });

  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    isDetail.value = !!data?.showFooter;
    isCopy.value = !!data?.isCopy;
    iD.value = data.record.id;
    console.log('isDetail', isDetail.value);
    console.log('isUpdate', isUpdate.value);
    console.log('isCopy', isCopy.value);
    console.log('data', data.record);

    if (unref(isUpdate)) {
      //表单赋值
      await setFieldsValue({
        ...data.record,
      });
    }
    // 隐藏底部时禁用整个表单
    setProps({ disabled: !data?.showFooter });
  });
  //设置标题
  const title = computed(() => '创建会员');
  //表单提交事件
  async function handleSubmit(v) {
    try {
      let values = await validate();
      values.cardTypeId = iD.value;

      setModalProps({ confirmLoading: true });
      //提交表单
      await createUser(values);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
  /** 时间和数字输入框样式 */
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-calendar-picker) {
    width: 100%;
  }
  .tags_box {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    .tags {
      margin: 4px 6px;
      background-color: rgb(236, 245, 255);
      display: inline-block;
      height: 32px;
      line-height: 30px;
      font-size: 12px;
      color: rgb(64, 158, 255);
      box-sizing: border-box;
      padding: 0px 10px;
      border-width: 1px;
      border-style: solid;
      border-color: rgb(217, 236, 255);
      border-image: initial;
      border-radius: 4px;
      white-space: nowrap;
      caret-color: transparent;
      cursor: pointer;
      .close {
        margin: 0 10px;
      }
    }
    .btn {
      height: 32px;
      line-height: 30px;
      padding-top: 0px;
      padding-bottom: 0px;
      border-width: 1px;
      font-size: 12px;
      border-style: solid;
      border-color: rgb(217, 236, 255);
      padding: 0px 10px;
      caret-color: transparent;
    }
  }
  .cp {
    cursor: pointer;
  }
</style>
